<?php
namespace Cli\Controller;
use \Think\Controller;

class DataController extends \Think\Controller {

    public function run() {
        if (!IS_CLI) {
            echo 'error';
            return;
        }
        
        $adminHotelModel = D('AdminHotel');
        $adminInfo = $adminHotelModel->where(array(
            'role' => 2,
            'isdelete' => NOT_DELETE,
        ))->select();

        $hotelIds = array_column($adminInfo, 'hotelentity_id');
        $hotelIds = array_unique($hotelIds);
        $roomInfoModel = D('RoomInfo');
        $roomInfo = $roomInfoModel->where(array(
            'hotelentity_id' => array('in', $hotelIds),
            'isdelete' => NOT_DELETE,
        ))->select();
        $roomInfo = getArrayValueAsIndex($roomInfo, 'hotelentity_id', 'multi');

        $sqlArray = array(
        );
        foreach ($adminInfo as $adminInfoItem) {
            foreach ($roomInfo[$adminInfoItem['hotelentity_id']] as $roomInfoItem) {
                array_push($sqlArray, array(
                    'adminlogin_id' => $adminInfoItem['adminlogin_id'],
                    'hotelentity_id' => $adminInfoItem['hotelentity_id'],
                    'roominfo_id' => $roomInfoItem['id'],
                ));
            }
            unset($roomInfoItem);
        }
        unset($adminInfoItem);

        $result = D('HotelRoomAuthority')->addAll($sqlArray) ? true : false;

        echo ($result ? 'success' : 'fail') . "\n";

        return;
   }
   //删除之前多对一的数据，重新绑定同步关系
   public function UpdateOldData() {
        if (!IS_CLI) {
            echo 'error';
            return;
        }
        $hotelEntityModel = D('HotelEntity');
        $roomInfoModel = D('RoomInfo');
        $waitToDelete = $hotelEntityModel->field('id,name,communityno,count(communityno) as chongfu')->where('communityno<>0')->group('communityno')->having('chongfu>1')->select();
        //dump($waitToDelete);die;
        foreach ($waitToDelete as $key => $oneData) {
            $option['communityno'] = $oneData['communityno'];
            $option['id'] = array('neq',224); //老友客栈
            $tempHotel = $hotelEntityModel->where($option)->select();
            foreach ($tempHotel as $key1 => $oneHotel) {
                $roomInfoModel->where(array('hotelentity_id'=>$oneHotel['id']))->save(array('roomfrom'=>0));
            }
            echo "Clear locktype when communityno = ".$option['communityno']."\n";

            $temp = $hotelEntityModel->where($option)->save(array('communityno'=>0));

            echo "set communityno = 0 where communityno = ".$option['communityno']."\n";
        }

        $hotelEntityInfo = $hotelEntityModel->getSynHotelIds();
        $hotelSynModel = D('HotelSyn');
        $adminHotelModel = D('AdminHotel');
        $adminLoginModel = D('AdminLogin');
        $roomSynModel = D('RoomSyn');
        Vendor('phpRPC.phprpc_client');
        $client = new \PHPRPC_Client(C('MANAGE_SYN_URL'));
        $client->setEncryptMode(3);
       
        foreach ($hotelEntityInfo as $key => $oneInfo) {
            if($hotelSynModel->getInfoByHotelId($oneInfo['id'])){
                continue;
            }
            $adminHotelInfo = $adminHotelModel->getHotelCreator($oneInfo['id']);
            $adminLoginInfo = $adminLoginModel->getInfo($adminHotelInfo['adminlogin_id']);
            $sendData = array(
                'flag' => 'uclbrt',
                'time' => time(),
                'communityNo' => $oneInfo['communityno']
            );
            $clientData = $client->getCommunityInfoByNo($sendData);

            $result = $hotelSynModel->addInfo($oneInfo['id'],$oneInfo['communityno'],$clientData['id'],$adminHotelInfo['adminlogin_id']);
            if(empty($result)){
                echo "SynTable import error";
                exit;
            }
            $sendData = array(
                'flag' => 'uclbrt',
                'time' => time(),
                'm_adminlogin_id' => $adminHotelInfo['adminlogin_id'],
                'm_adminlogin_mobile' => $adminLoginInfo['mobile'],
                'm_hotelentity_id' => $oneInfo['id'],
                'm_hotelentity_name' => $oneInfo['name'],
                'q_communityentity_id' => $clientData['id'],
            );
            $rpcData = $client->notifyCommunitySynchronized($sendData);
            if (empty($rpcData)) {
                echo "RPC import error";
                exit;
            }
            echo " Success to import hotelId = ".$oneInfo['id'].", communityId = ".$clientData['id']."\n";

            $option = array(
                    'hotelentity_id' => $oneInfo['id'],
                    'roomfrom' => array('neq',0),
                );
            $tempRoomInfo = $roomInfoModel->where($option)->select();

            foreach ($tempRoomInfo as $key1 => $oneRoomInfo) {
                $sendData = array(
                    'flag' => 'uclbrt',
                    'time' => time(),
                    'communityId' => $clientData['id'],
                    'no' => $oneRoomInfo['name'],
                );
                $rpcRoomData = $client->getRoomInfoById($sendData);
                $roomSynModel->addInfo($clientData['id'],$rpcRoomData['builderinfo_id'],$rpcRoomData['floorinfo_id'],$rpcRoomData['id'],$oneInfo['id'],$oneRoomInfo['id']);
                echo "Success to import roomName = ".$oneRoomInfo['name']."\n";
            }

        }
        echo "Complete imports";
   }

    // 添加客栈支付方式
    public function addHotelMemberCard() {
        if (!IS_CLI) {
            echo 'cli error';
            return;
        }

        // 获取所有客栈id
        $hotelEntityModel = D('HotelEntity');
        $hotelArrayId = $hotelEntityModel->field('id')->select();
        $hotelArrayId = array_column($hotelArrayId, 'id');
        // 构造插入数据
        $data = array();
        foreach ($hotelArrayId as $key => $hotelId) {
            array_push($data, array(
                'hotelentity_id' => $hotelId,
                'name' => '会员卡/Member card/會員卡',
                'type' => 1,
                'creator' => 0,
                'operator' => 0,
                'createtime' => date('Y-m-d H:i:s'),
                'updatetime' => date('Y-m-d H:i:s'),
                'status' => 0,
            ));
        }

        $hotelPayModel = D('HotelPay');
        $result = false;
        if (!empty($data)) {
            $result = $hotelPayModel->addAll($data);
        }

        if ($result == false) {
            echo "error";
            return;
        }

        echo "success";
        return;
    }

    // 添加客栈记一笔项目
    public function addHotelNote() {
        if (!IS_CLI) {
            echo 'cli error';
            return;
        }

        // 获取所有客栈id
        $hotelEntityModel = D('HotelEntity');
        $hotelArrayId = $hotelEntityModel->field('id')->select();
        $hotelArrayId = array_column($hotelArrayId, 'id');

        // 系统记一笔项目
        $noteModel = D('Note');
        $notes = $noteModel->getAll();

        // 构造插入数据
        $data = array();
        foreach ($hotelArrayId as $key => $hotelId) {
            foreach ($notes as $key => $note) {
                array_push($data, array(
                    'hotelentity_id' => $hotelId,
                    'name' => $note['name'],
                    'type' => $note['type'],
                ));
            }
        }

        $hotelNoteModel = D('HotelNote');
        $result = false;
        if (!empty($data)) {
            $result = $hotelNoteModel->addAll($data);
        }

        if ($result == false) {
            echo "error";
            return;
        }

        echo "success";
        return;
    }
}